/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package udio;
/**
 *
 * @author pat
 */
import java.sql.*;
public class UserDB {
    
    private DB db;

    public UserDB() {
        this.db = new DB();
    }
    
    public int getExpiryDate(int custID){
        int date = -1;
        Connection conn = db.connect();
        String query = "SELECT membershipEndDate FROM UserDB WHERE customerId = " + custID;
        ResultSet result = db.getQuery(conn,query);
        db.disconnect(conn);
        try{
            result.next();
            date = result.getInt("membershipEndDate");
        }catch(Exception e){ }
        return date;
    }
    
    public int getHoursLeft(int custID){
        int hours = -1;
        Connection conn = db.connect();
        String query = "SELECT hoursLeft FROM UserDB WHERE custId = " + custID;
        ResultSet result = db.getQuery(conn,query);
        db.disconnect(conn);
        try{
            result.next();
            hours = result.getInt("hoursLeft");
        }catch(Exception e){ }
        return hours;
    }
    
    public void updateHours(int custID, int hours){
        Connection conn = db.connect();
        String query = "UPDATE UserDB SET hoursLeft = " + hours
                + " WHERE customerId = " + custID;
        db.putQuery(conn, query);
        db.disconnect(conn);
    }
    
    public String[] getCustomerInfo(int custID){
        String[] info = new String[8];
        Connection conn = db.connect();
        String query = "SELECT customerId,username,membershipStartDate,membershipEndDate,address,phone,usertype,paymentInfo FROM UserDB WHERE customerId = " + custID;
        ResultSet result = db.getQuery(conn,query);
        db.disconnect(conn);
        try{
            result.next();
            info[0] = result.getInt("customerId")+"";
            info[1] = result.getString("username");
            info[2] = result.getInt("membershipStartDate")+"";
            info[3] = result.getInt("membershipEndDate")+"";
            info[4] = result.getString("address");
            info[5] = result.getString("phone");
            info[6] = result.getInt("userType")+"";
            info[7] = result.getString("paymentInfo");
        }catch(Exception e){ }
        return info;
    }
}
